package org.comet.auxiliary.client.liaobei.util;

import java.util.ArrayList;
import java.util.List;

/**
 * @author wq
 * @date 2019/10/6
 * @description template
 */
public class MsqUtil {

    public static int s = -1;

    private static String t = "Ml1A&Yx<D5Q8-5gY/KpxrK@z^;O+n[uIpW\"h:JN;dt4/P=:44cy@`Cfn)z^8=eAt";
    public static final String I = "l";

    public static String getNextMsqId() {
        return hashCode(++s, t);
    }

    public static void setMsq(String msqsid) {
        MsqUtil.s = hashDecode(msqsid, t);
    }

    public static String hashCode(int seq, String t) {
        String e = Integer.toString(seq);
/*
                if (!e || !t) return "";
                e = String(e), t = String(t);
                for (var n = [], r = e.length, o = t.length, a = 0, s = 0; r > a; a++)
                    n.push(e.charCodeAt(a) ^ t.charCodeAt(s)), s = s % o + 1;
                return n.join(i)
 */
        List<Integer> n = new ArrayList<>(20);
        for (int r = e.length(), o = t.length(), a = 0, s = 0; r > a; a++) {
            int res = e.charAt(a) ^ t.charAt(s);
            n.add(res);
            s = s % o + 1;
        }
        return join(n, I);
    }

    public static String join(List array, String str) {
        StringBuilder sbf = new StringBuilder();
        for (int i = 0; i < array.size(); i++) {
            Object item = array.get(i);
            sbf.append(item);
            if (i != array.size() - 1) {
                sbf.append(str);
            }
        }
        return sbf.toString();
    }

    public static int hashDecode(String e, String t) {
        /*
                if (!e || !t) return "";
                e = String(e), t = String(t);
                var n = t.length, o = e.split(i), a = 0;
                return r.reduce(o, function (e, r) {
                    return e += String.fromCharCode(parseInt(r, 10) ^ t.charCodeAt(a)), a = a % n + 1, e
                }, "")
         */
        int n = t.length(), a = 0;
        String[] o = e.split(I);
        StringBuilder e0 = new StringBuilder();
        for (String r : o) {
            e0.append((char) (Integer.parseInt(r) ^ t.charAt(a)));
            a = a % n + 1;
        }
        return Integer.parseInt(e0.toString());
    }
}
